package com.zj.osm.adapter

import android.content.Context
import android.os.Build
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import com.zj.osm.R
import com.zj.osm.entity.greendao.Line
import java.util.function.Consumer


/**
 * <pre>
 *     author : luhenchang
 *     e-mail : 1276998208@qq.com
 *     time   : 2020/04/10
 *     desc   : 版权所有
 *     version: 1.0
 * </pre>
 */
class TDialogReAdapter(context: Context, list: List<Line>) : RecyclerView.Adapter<TDialogReAdapter.MyRecyclerHolder?>() {
    private val mContext: Context = context
    private val list: List<Line> = list
    private var tLineId = ""
    fun getTLineId(): String? {
        if ((tLineId == null || tLineId.isEmpty()) && list.size > 0) {
            for (i in list.indices) {
                if (list[i].fenZhiLineChecked) {
                    return list[i].cid
                }
            }
        }
        return tLineId
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TDialogReAdapter.MyRecyclerHolder {
        val view: View = LayoutInflater.from(mContext).inflate(com.zj.osm.R.layout.activity_map_t_dialog, null);
        return MyRecyclerHolder(view)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    inner class MyRecyclerHolder(rootView: View) : RecyclerView.ViewHolder(rootView) {
        var adapter_tv: TextView
        var adapter_cb: CheckBox

        init {
            adapter_tv = rootView.findViewById(R.id.adapter_tv)
            adapter_cb = rootView.findViewById(R.id.adapter_cb)
        }
    }

    @RequiresApi(Build.VERSION_CODES.N)
    override fun onBindViewHolder(viewHolder: TDialogReAdapter.MyRecyclerHolder, position: Int) {
        viewHolder.adapter_tv.setText(list[position].name)
        if (list[position].fenZhiLineChecked) {
            viewHolder.adapter_cb.setChecked(true)
        } else {
            viewHolder.adapter_cb.setChecked(false)
        }
        viewHolder.adapter_cb.setOnClickListener(View.OnClickListener {
            list.forEach(Consumer { line: Line -> line.fenZhiLineChecked = false })
            list[position].fenZhiLineChecked = true
            Log.e("lineId=", list[position].toString())
            tLineId = list[position].cid
            notifyDataSetChanged()
        })
    }
}

        